// Copyright (c) MatrixFramework. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Application.Dtos;
using Volo.Abp.AspNetCore.Mvc;
using PhotoGalleryService.Application.Contracts.Photos;
using AbpMvcController = Volo.Abp.AspNetCore.Mvc.AbpControllerBase;

namespace PhotoGalleryService.Photos;

/// <summary>
/// 照片管理API控制器
/// </summary>
[ApiController]
[Route("api/photos")]
public class PhotoController : AbpMvcController
{
    private readonly IPhotoAppService _photoAppService;

    public PhotoController(IPhotoAppService photoAppService)
    {
        _photoAppService = photoAppService;
    }

    /// <summary>
    /// 获取照片列表
    /// </summary>
    /// <param name="input">分页请求参数</param>
    /// <returns>照片分页列表</returns>
    [HttpGet]
    public async Task<PagedResultDto<PhotoDto>> GetListAsync(PagedAndSortedResultRequestDto input)
    {
        return await _photoAppService.GetListAsync(input);
    }

    /// <summary>
    /// 根据ID获取照片
    /// </summary>
    /// <param name="id">照片ID</param>
    /// <returns>照片信息</returns>
    [HttpGet("{id}")]
    public async Task<PhotoDto> GetAsync(Guid id)
    {
        return await _photoAppService.GetAsync(id);
    }

    /// <summary>
    /// 创建照片
    /// </summary>
    /// <param name="input">创建照片输入参数</param>
    /// <returns>创建的照片信息</returns>
    [HttpPost]
    public async Task<PhotoDto> CreateAsync(CreateUpdatePhotoDto input)
    {
        return await _photoAppService.CreateAsync(input);
    }

    /// <summary>
    /// 更新照片
    /// </summary>
    /// <param name="id">照片ID</param>
    /// <param name="input">更新照片输入参数</param>
    /// <returns>更新后的照片信息</returns>
    [HttpPut("{id}")]
    public async Task<PhotoDto> UpdateAsync(Guid id, CreateUpdatePhotoDto input)
    {
        return await _photoAppService.UpdateAsync(id, input);
    }

    /// <summary>
    /// 删除照片
    /// </summary>
    /// <param name="id">照片ID</param>
    /// <returns></returns>
    [HttpDelete("{id}")]
    public async Task DeleteAsync(Guid id)
    {
        await _photoAppService.DeleteAsync(id);
    }

    /// <summary>
    /// 上传单个照片
    /// </summary>
    /// <param name="input">上传输入参数</param>
    /// <returns>上传的照片信息</returns>
    [HttpPost("upload")]
    public async Task<PhotoDto> UploadAsync(PhotoUploadInput input)
    {
        return await _photoAppService.UploadAsync(input);
    }

    /// <summary>
    /// 下载照片
    /// </summary>
    /// <param name="id">照片ID</param>
    /// <returns>照片下载信息</returns>
    [HttpGet("{id}/download")]
    public async Task<PhotoDownloadDto> DownloadAsync(Guid id)
    {
        return await _photoAppService.DownloadAsync(id);
    }

    /// <summary>
    /// 获取照片内容
    /// </summary>
    /// <param name="id">照片ID</param>
    /// <param name="size">图片尺寸</param>
    /// <returns>文件流</returns>
    [HttpGet("{id}/content")]
    public async Task<byte[]> GetContentAsync(Guid id, string size = "original")
    {
        return await _photoAppService.GetContentAsync(id, size);
    }

    /// <summary>
    /// 批量上传照片
    /// </summary>
    /// <param name="inputs">上传输入参数列表</param>
    /// <returns>照片信息列表</returns>
    [HttpPost("batch-upload")]
    public async Task<List<PhotoDto>> BatchUploadAsync(List<PhotoUploadInput> inputs)
    {
        return await _photoAppService.BatchUploadAsync(inputs);
    }

    /// <summary>
    /// 根据哈希查找照片
    /// </summary>
    /// <param name="fileHash">文件哈希</param>
    /// <returns>照片信息</returns>
    [HttpGet("by-hash/{fileHash}")]
    public async Task<PhotoDto?> FindByHashAsync(string fileHash)
    {
        return await _photoAppService.FindByHashAsync(fileHash);
    }

    /// <summary>
    /// 搜索照片
    /// </summary>
    /// <param name="input">搜索输入参数</param>
    /// <returns>搜索结果</returns>
    [HttpPost("search")]
    public async Task<PagedResultDto<PhotoDto>> SearchAsync(PhotoSearchInput input)
    {
        return await _photoAppService.SearchAsync(input);
    }

    /// <summary>
    /// 获取我的照片
    /// </summary>
    /// <param name="input">分页请求</param>
    /// <returns>分页结果</returns>
    [HttpGet("my-photos")]
    public async Task<PagedResultDto<PhotoDto>> GetMyPhotosAsync(PagedAndSortedResultRequestDto input)
    {
        return await _photoAppService.GetMyPhotosAsync(input);
    }

    /// <summary>
    /// 获取公开照片
    /// </summary>
    /// <param name="input">分页请求</param>
    /// <returns>分页结果</returns>
    [HttpGet("public")]
    public async Task<PagedResultDto<PhotoDto>> GetPublicPhotosAsync(PagedAndSortedResultRequestDto input)
    {
        return await _photoAppService.GetPublicPhotosAsync(input);
    }
}